package com.gm.wj.controller;

import com.gm.wj.entity.Book;
import com.gm.wj.entity.SysImage;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.BookService;
import com.gm.wj.service.SysImageService;
import com.gm.wj.util.MyTime;
import com.gm.wj.util.StringUtils;
import com.gm.wj.util.SysLoging;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

/**
* @Description: 图书馆
* @Author: Huang_shengjun
* @time: 2020/8/12 22:51
*/
@Api(tags = "图书馆管理相关接口")
@Log4j2
@RestController
public class LibraryController {
    @Autowired
    BookService bookService;
    @Autowired
    SysImageService sysImageService;

    // 图书馆列表
    @GetMapping("/api/books")
    @SysLoging("图书馆列表信息")
    public Result listBooks() {
        return ResultFactory.buildSuccessResult("查询成功",bookService.list());
    }

    @PostMapping("/api/admin/content/books")
    @SysLoging("添加图书馆列表信息")
    public Result addOrUpdateBooks(@RequestBody @Valid Book book) {
        try {
            bookService.addOrUpdate(book);
            return ResultFactory.buildSuccessResult("添加/修改成功",null);
        } catch (Exception e){
            return ResultFactory.buildFailResult("添加/修改失败");
        }
    }

    @SysLoging("删除图书馆信息")
    @PostMapping("/api/admin/content/books/delete")
    public Result deleteBook(@RequestBody @Valid Book book) {
        bookService.deleteById(book.getId());
        return ResultFactory.buildSuccessResult("删除成功",null);
    }

    @GetMapping("/api/search")
    @SysLoging("根据关键字查询图书馆信息")
    public Result searchResult(@RequestParam("keywords") String keywords) {
        if ("".equals(keywords)) {
            return ResultFactory.buildSuccessResult("查询成功",bookService.list());
        } else {
            return ResultFactory.buildSuccessResult("查询成功",bookService.Search(keywords));
        }
    }

    @GetMapping("/api/categories/{cid}/books")
    @SysLoging("某个目录下图书馆列表信息")
    public Result listByCategory(@PathVariable("cid") int cid) {
        if (0 != cid) {
            return ResultFactory.buildSuccessResult("查询成功",bookService.listByCategory(cid));
        } else {
            return ResultFactory.buildSuccessResult("查询成功",bookService.list());
        }
    }


//    @PostMapping("/api/admin/content/books/covers")
//    @SysLoging("图书馆图片上传")
//    public String imageUpload(MultipartFile file) {
//        return new ImageUtil().imgUpload(file,"图书馆图片");
//    }

    //读取图片地址
    @Value("${image.url}")
    String ima_folder;

    @PostMapping("/api/admin/content/books/covers")
    @SysLoging("图书馆图片上传")
    public Result coversUpload(MultipartFile file) {
        File imageFolder = new File(ima_folder);
        File f = new File(imageFolder, StringUtils.getRandomString(6) + file.getOriginalFilename()
                .substring(file.getOriginalFilename().length() - 4));
        if (!f.getParentFile().exists())
            f.getParentFile().mkdirs();
        try {
            file.transferTo(f);
            String imgURL = "http://localhost:8090/api/file/" + f.getName();

            SysImage sysImage = new SysImage();
            sysImage.setImageId(MyTime.getRandom(6));
            sysImage.setUrl(imgURL);
            sysImage.setFolder(ima_folder);
            sysImage.setImgType("图片上传");
            log.info(sysImage.toString());
            sysImageService.add(sysImage);

            return ResultFactory.buildSuccessResult("成功",imgURL);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultFactory.buildFailResult("服务器出错了，请联系管理员。错误信息：" + e.getMessage());
        }
    }

}
